package com.mbc.common.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import com.mbc.common.entities.UserSettings;

public class UserSettingsDAOImpls extends DAO<UserSettings> implements UserSettingsDAO {

	public UserSettingsDAOImpls(Connection conn)
	{
		this.conn = conn;
		this.table_name = "UserSettings";
	}

	@Override
	public void create(UserSettings entity) throws SQLException {
		query = "insert into " + table_name + " ( UserID, RowNo, Language, Theme, FormBackground) values( ?, ?, ?, ?, ?)";
		pstmt = conn.prepareStatement(query);
		int i = 1;
				pstmt.setLong(i++, entity.getUserID());
		pstmt.setInt(i++, entity.getRowNo());
		pstmt.setString(i++, entity.getLanguage());
		pstmt.setString(i++, entity.getTheme());
		pstmt.setString(i++, entity.getFormBackground());

		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void update(UserSettings entity) throws SQLException {
		query = "update " + table_name + " set  RowNo = ?, Language = ?, Theme = ?, FormBackground = ?";
		pstmt = conn.prepareStatement(query);
		int i = 1;
				pstmt.setInt(i++, entity.getRowNo());
		pstmt.setString(i++, entity.getLanguage());
		pstmt.setString(i++, entity.getTheme());
		pstmt.setString(i++, entity.getFormBackground());

		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void delete(long key) throws SQLException {
		query = getDeleteQuery();
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public UserSettings find(long key) throws SQLException {
		UserSettings l_UserSettings = null;
		query = getSelectQuery();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if(rs.next())
			l_UserSettings = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_UserSettings;
	}

	@Override
	public ArrayList<UserSettings> findAll() throws SQLException {
		ArrayList<UserSettings> l_UserSettingsList = new ArrayList<UserSettings>();
		query = getSelectQuery();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			l_UserSettingsList.add(readRecord(rs));
		}
		rs.close();
		pstmt.close();
		return l_UserSettingsList;
	}

	@Override
	protected UserSettings readRecord(ResultSet rs) throws SQLException {
		UserSettings l_entity = new UserSettings();
				l_entity.setUserID(rs.getLong("UserID"));
		l_entity.setRowNo(rs.getInt("RowNo"));
		l_entity.setLanguage(rs.getString("Language"));
		l_entity.setTheme(rs.getString("Theme"));
		l_entity.setFormBackground(rs.getString("FormBackground"));

		return l_entity;
	}
}
